﻿using System.Windows.Input;
using TestBat.IModels.Domain;
using TestBat.IModels.Models;
using TestBat.IViewModels.Workspaces.Tasks;
using TestBat.ViewModels.Attributes;
using TestBat.ViewModels.Base;
using TestBat.ViewModels.Base.Commands;

namespace TestBat.ViewModels.Workspaces.Tasks
{
    public abstract class TaskViewModel : TreeItemViewModel, ITaskViewModel
    {
        public TaskViewModel(ITask task, ITaskModel taskModel)
        {
            this.taskModel = taskModel;
            this.task = task;
        }

        public ITask Task
        {
            get { return task; }
        }

        public virtual ICommand SaveCommand
        {
            get { return new RelayCommand(c => Save()); }
        }

        public virtual ICommand DeleteCommand
        {
            get { return new RelayCommand(c => Delete()); }
        }

        [BusinessErrorDialog]
        public override void OnLostFocus()
        {
            // revert changes
            taskModel.Revert(task);
        }

        [BusinessErrorDialog]
        [ValidationErrorDialog]
        private void Save()
        {
            taskModel.Save(task);
        }

        [YesNoDialog("Do you really want to delete this task?")]
        [BusinessErrorDialog]
        private void Delete()
        {
            taskModel.Delete(task);

            if (OnRemove != null)
                OnRemove(this);
        }

        private ITask task;
        protected readonly ITaskModel taskModel;
    }
}
